package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

//流对象获取数据
@WebServlet("/servletDemo9")
public class servletDemo9 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath = getServletContext().getRealPath("/img/4(1).jpg");
        System.out.println(realPath);
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(realPath));
        ServletOutputStream os = resp.getOutputStream();
        byte []chars=new byte[1024];
        int c;
        while ((c=bis.read(chars))!=-1){
            os.write(chars,0,c);
        }
        bis.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
